package com.wechat.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.wechat.core.model.ArticleMaterial;
import com.wechat.core.service.AbstractService;
import com.wechat.core.service.WxMediaService;
import com.wechat.core.util.HttpClientUtils;
import com.wechat.core.util.R;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 素材管理
 *
 * @author hgb
 * @date 2019/1/27
 * Email xhy18650@sina.com
 */
@Service
public class WxMediaServiceImpl extends AbstractService implements WxMediaService {


    @Override
    public R uploadMedia(MultipartFile media, String type) {
        if (media.isEmpty()) {
            R.error("素材不能不空");
        }
        if (type == null || "".equals(type)) {
            R.error("选择素材类型");
        }
        try {
            //转换为本地文件
            File targetFile = saveDisk(media);
            //上传到微信
            Map<String, Object> params = getParams();
            params.put("type", type);
            String result = HttpClientUtils.uploadMedia(UPLOAD_MEDIA_URL, params, targetFile);
            Map<String, Object> map = JSON.parseObject(result);
            //TODO 保存到本地数据库 media_id
            return R.ok(map);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("errer{}", e);
        }
        return R.error("未知错误");
    }

    @Override
    public R getMedia(String media_id) {
        try {
            Map<String, Object> params = getParams();
            params.put("media_id", media_id);
            String result = HttpClientUtils.httpGetRequest(GET_MEDIA_URL, params);
            Map<String, Object> map = JSON.parseObject(result);
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("未知错误");
    }

    @Override
    public R addNewsMaterial(Map<String, Object> params) {
        try {
            params.putAll(params);
            List<ArticleMaterial> articleMaterials = new ArrayList<>();
            ArticleMaterial articleMaterial = new ArticleMaterial();
            articleMaterial.setTitle("小胖子测试");
            articleMaterial.setThumb_media_id("jL7w_crYkKu-5l0nLh1Ou7_5Icd6uqV0i16NFF713a0");//封面
            articleMaterial.setAuthor("小胖子");
            articleMaterial.setDigest("");
            articleMaterial.setShow_cover_pic(1);
            articleMaterial.setContent("这是content");
            articleMaterial.setContent_source_url("http://www.baidu.com");
            articleMaterial.setNeed_open_comment(1);
            articleMaterial.setOnly_fans_can_comment(1);
            articleMaterials.add(articleMaterial);
            params.put("articles", articleMaterials);
            String result = HttpClientUtils.httpPostJSON(
                    ADD_NEWS_MATERIAL_URL + "?access_token=" + getParams().get("access_token")
                    , JSON.toJSONString(params));
            Map<String, Object> map = JSON.parseObject(result);
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("未知错误");
    }

    @Override
    public R uploadimgMedia(MultipartFile media, Map<String, Object> params) {
        try {
            params.putAll(params);
            params.putAll(getParams());
            String result = HttpClientUtils.uploadMedia(UPLOAD_MEDIA_URL, params, saveDisk(media));
            Map<String, Object> map = JSON.parseObject(result);
            return R.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("未知错误");
    }

    @Override
    public R addMaterialMaterial(MultipartFile media, Map<String, Object> params) {
        //   新增其他类型永久素材
        try {
            params.putAll(params);
            params.putAll(getParams());
            String result = HttpClientUtils.uploadMedia(ADD_MATERIAL_MATERIAL_URL, params, saveDisk(media));
            return R.ok(JSON.parseObject(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("未知错误");
    }

    @Override
    public R getMaterialMaterial(Map<String, Object> params) {
        // 获取永久素材
        try {
            String josnStr = JSON.toJSONString(params);
            String result = HttpClientUtils.httpPostJSON(GET_MATERIAL_MATERIAL_URL + "?access_token=" + getParams(), josnStr);
            return R.ok(JSON.parseObject(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("未知错误");
    }

    @Override
    public R delMaterialMaterial(Map<String, Object> params) {
        // 删除永久素材
        try {
            String result = HttpClientUtils.httpPostJSON(
                    DEL_MATERIAL_MATERIAL_URL + "?access_token=" + getParams()
                    , JSON.toJSONString(params));
            return R.ok(JSON.parseObject(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("未知错误");
    }

    @Override
    public R updateNewsMaterial(Map<String, Object> params) {
        // 修改永久图文素材
        try {
            List<ArticleMaterial> articleMaterials = new ArrayList<ArticleMaterial>();
            ArticleMaterial articleMaterial = new ArticleMaterial();
            articleMaterial.setTitle("小胖子测试");
            articleMaterial.setThumb_media_id("jL7w_crYkKu-5l0nLh1Ou7_5Icd6uqV0i16NFF713a0");//封面
            articleMaterial.setAuthor("小胖子01");
            articleMaterial.setDigest("");
            articleMaterial.setShow_cover_pic(1);
            articleMaterial.setContent("这是content");
            articleMaterial.setContent_source_url("http://www.baidu.com");
            articleMaterial.setNeed_open_comment(1);
            articleMaterial.setOnly_fans_can_comment(1);
            logger.debug(articleMaterial.toString());
            articleMaterials.add(articleMaterial);
            params.put("articles", articleMaterials);
            String jsonStr = JSON.toJSONString(params);
            String result = HttpClientUtils.httpPostJSON(
                    UPDATE_NEWS_MATERIAL_URL + "?access_token=" + getParams().get("access_token")
                    , jsonStr);
            return R.ok(JSON.parseObject(result));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.error("未知错误");
    }

    @Override
    public R getMaterialcountMaterial(Map<String, Object> params) {
        // 获取素材总数
        try {
           String result =  HttpClientUtils.httpGetRequest(GET_MATERIALCOUNT_MATERIAL_URL,getParams());
           return R.ok(JSON.parseObject(result));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error("未知错误");
    }

    @Override
    public R batchgetMaterialMaterial(Map<String, Object> params) {
        // 获取素材列表
        try {
            params.putAll(params);
            String result = HttpClientUtils.httpPostJSON(
                    BATCHGET_MATERIAL_MATERIAL_URL+"?access_token=" + getParams().get("access_token"),
                    JSON.toJSONString(params));
            return R.ok(JSON.parseObject(result));
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error("未知错误");
    }
}
